﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Media;

namespace simu485.Data
{
    public class C485Cmds
    {
        public static string GetDesByCmd(ushort cmd)
        {
            switch (cmd >> 8)
            {     // 根据cmd的高位分块检索, 加快检索速度
                case 0x01:
                    switch ((byte)cmd)
                    {
                        case 0x01:
                            return "通信监测";
                        case 0x02:
                            return "无效指令应答";
                        case 0x03:
                            return "自动发送指令应答";
                        case 0x11:
                            return "设备重启";
                        case 0x12:
                            return "程序升级开始";
                        case 0x13:
                            return "程序升级传送";
                        case 0x14:
                            return "程序版本取得";
                        case 0x15:
                            return "硬件版本取得";
                        case 0x16:
                            return "厂家代码取得";

                        case 0x21:
                            return "设备类型取得";
                        case 0x22:
                            return "设备功能取得";
                        case 0x23:
                            return "系统时间设定";
                        case 0x24:
                            return "系统时间取得";
                        case 0x25:
                            return "运行状态取得";
                        case 0x26:
                            return "现在故障解除";
                        case 0x27:
                            return "现在故障状态取得";
                        case 0x28:
                            return "压缩机实时状态取得";
                        case 0x29:
                            return "门状态取得";
                        case 0x2A:
                            return "灯状态取得";
                        case 0x2B:
                            return "功率值取得";
                        case 0x2C:
                            return "电池电压值取得";
                        case 0x2D:
                            return "联网状态取得";
                        case 0x2E:
                            return "冰柜蒸发风机状态设定";
                        case 0x2F:
                            return "冰柜蒸发风机状态取得";
                        case 0x30:
                            return "灯状态设定";
                        case 0x31:
                            return "灯电流取得";
                        case 0x32:
                            return "4G 信号值取得";
                        default:
                            return "无效指令"; // cmd not support
                    }
                case 0x02:                          // 基本数据相关
                    switch ((byte)cmd)
                    {
                        case 0x01:
                            return "库制冷/制热模式设定";
                        case 0x02:
                            return "库制冷/制热模式取得";
                        case 0x03:
                            return "库设置温度设定";
                        case 0x04:
                            return "库设置温度取得";
                        case 0x05:
                            return "库高温报警温度设定";
                        case 0x06:
                            return "库高温报警温度取得";
                        case 0x07:
                            return "库低温报警温度设定";
                        case 0x08:
                            return "库高低报警温度取得";
                        case 0x09:
                            return "库当前温度取得(显示温度)";
                        case 0x0A:
                            return "库设置温度下限设定";
                        case 0x0B:
                            return "库设置温度下限取得";
                        case 0x0C:
                            return "库设置温度上限设定";
                        case 0x0D:
                            return "库设置温度上限取得";
                        case 0x0E:
                            return "库温度上回差取得";
                        case 0x0F:
                            return "库温度下回差取得";
                        case 0x10:
                            return "库传感器故障压缩机开机时间取得";
                        case 0x11:
                            return "库传感器故障压缩机关机时间取得";
                        case 0x12:
                            return "库温度补偿值设定";
                        case 0x13:
                            return "库温度补偿值取得";
                        case 0x14:
                            return "库除霜周期/压缩机保护周期取得";
                        case 0x15:
                            return "库除霜时间/压缩机保护时间取得";
                        case 0x16:
                            return "库压缩机最小停机时间取得";
                        case 0x17:
                            return "库环境温度取得";
                        case 0x18:
                            return "库实际温度（温度传感器测量温度）取得";
                        case 0x19:
                            return "库本地控制状态取得";
                        default:
                            return "无效指令"; // cmd not support
                    }
                case 0x05:                          // 统计数据相关
                    switch ((byte)cmd)
                    {
                        case 0x01:
                            return "开门次数清空";
                        case 0x02:
                            return "开门次数取得";
                        case 0x03:
                            return "客流次数清空";
                        case 0x04:
                            return "客流次数取得";
                        default:
                            return "无效指令"; // cmd not support
                    }
                case 0x06:                          // 节能照明相关, 本APP不支持
                default:
                    return "无效指令"; // cmd not support
            }
        }



            /*
                    public ushort cmd_num = 0;          // 指令编号
                    public string cmd_des = "";         // 指令描述
                    public string value = "";           // 发送值
                    public int value_type;              // 发送值类型
                    public C485Param? pm = null;        // 参数绑定

                    public byte[] send_buf = new byte[64];
                    public int send_len = 0;
                    public byte[] recv_buf = new byte[64];
                    public int recv_len = 0;

                    public C485Cmds(ushort cmd_num, string cmd_des, string value, int value_type)
                    {
                        this.cmd_num = cmd_num;
                        this.cmd_des = cmd_des;
                        this.value = value;
                        this.value_type = value_type;
                    }

                    public string? PackSendData(string new_value)
                    {
                        if (new_value.Length > 48 || new_value.Length < 2) // TODO 目前指支持指令值长度小于48，总体长度小于64
                            return null;
                        byte[]? value_bytes = null;
                        if (value_type != 4)
                        {
                            string[] strs = new_value.Split(" ");
                            if((value_type == 2 && (strs.Length % 2) != 0) || (value_type == 3 && (strs.Length % 4) != 0))
                                return null;
                            value_bytes = new byte[strs.Length];
                            for(int i = 0; i < value_bytes.Length; i++)
                            {
                                if (!byte.TryParse(strs[i], System.Globalization.NumberStyles.HexNumber, null, out value_bytes[i]))
                                    return null;
                            }
                        }
                        // 开始构造发送数据
                        send_len = 0;
                        // 例子 "DD 00 09 01 02 03 01 01 01 00 01 50 59 88";
                        send_buf[send_len++] = 0xdd;            // head flag
                        send_buf[send_len++] = 0x00;            // len high byte
                        byte len_low_index = (byte)send_len;    // record this position
                        send_buf[send_len++] = 0x00;            // len low byte
                        send_buf[send_len++] = 0x01;            // mechine number
                        send_buf[send_len++] = (byte)(cmd_num >> 8); // cmd number
                        send_buf[send_len++] = (byte)cmd_num;
                        send_buf[send_len++] = 0x01;            // lib num
                        send_buf[send_len++] = 0x01;            // block num
                        send_buf[send_len++] = (byte)value_type;// data type

                        send_buf[send_len++] = 0x00;                     // data number high byte
                        if (value_type == 1)
                            send_buf[send_len++] = (byte)(value_bytes.Length); // data number low byte
                        else if (value_type == 2)
                            send_buf[send_len++] = (byte)(value_bytes.Length / 2); // data number low byte
                        else if (value_type == 3)
                            send_buf[send_len++] = (byte)(value_bytes.Length / 4); // data number low byte
                        else
                            send_buf[send_len++] = (byte)(new_value.Length); // data number low byte


                        if (value_type == 4)
                        {
                            foreach (char ch in new_value)
                                send_buf[send_len++] = (byte)ch;
                        }
                        else
                        {
                            for (int i = 0; i < value_bytes?.Length;)
                                send_buf[send_len++] = value_bytes[i++];
                        }

                        send_buf[len_low_index] = (byte)(send_len - 3); // 减去head flag, len本身
                        byte s = 0;
                        for (int i = 1; i < send_len; i++)
                        {
                            s ^= send_buf[i];
                        }
                        send_buf[send_len++] = s; // 预备放checksum
                        send_buf[send_len++] = 0x88; // tail flag

                        return BitConverter.ToString(send_buf, 0, send_len).Replace("-", " ");
                    }

                    public string? UnpackRecvData(string data)
                    {
                        return null;
                    }
            */
    }
}
